#ifndef _NE_MUTEX_H_
#define _NE_MUTEX_H_

#include <neinit.h>
#include <pthread.h>

namespace ne
{
    typedef pthread_mutex_t MutexID;
    typedef pthread_mutex_t* MutexPtr;

    class NEAPI Mutex
    {
        public:
            Mutex();

            bool isLocked() const;

            bool lock();

            bool trylock();

            bool unlock();

            ~Mutex();

        private:
            Mutex(const Mutex &mutex);

            Mutex& operator = (const Mutex &mutex);


        private:
            MutexPtr mpMutex;
            volatile bool mIsLocked;
    };

    class NEAPI MutexLocker
    {
        public:
            MutexLocker(const Mutex *pMutex)
                : mpMutex(const_cast<Mutex*>(pMutex))
            {
                if (mpMutex)
                {
                    mpMutex->lock();
                }
            }

            ~MutexLocker()
            {
                if (mpMutex)
                {
                    mpMutex->unlock();
                }
            }

        private:
            MutexLocker(const MutexLocker &mutex);

            MutexLocker& operator = (const MutexLocker &mutex);

        private:
            mutable Mutex *mpMutex;
    };
}

#endif
